package com.tangyanbo.filemanager.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Util {
	
	private final static String ZIP = ".zip";
	
	public static boolean checkEndingIsApk(String fileName){
		if(fileName.endsWith(".apk")){
			return true;
		}
		return false;
	}
	
	public static boolean deleteFolder(File folder) {
		boolean result = false;
		try {
			String childs[] = folder.list();
			if (childs == null || childs.length <= 0) {
				if (folder.delete()) {
					result = true;
				}
			} else {
				for (int i = 0; i < childs.length; i++) {
					String childName = childs[i];
					String childPath = folder.getPath() + File.separator
							+ childName;
					File filePath = new File(childPath);
					if (filePath.exists() && filePath.isFile()) {
						if (filePath.delete()) {
							result = true;
						} else {
							result = false;
							break;
						}
					} else if (filePath.exists() && filePath.isDirectory()) {
						if (deleteFolder(filePath)) {
							result = true;
						} else {
							result = false;
							break;
						}
					}
				}
				folder.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}
	
	public static void copyFile(File src, File target) {
		InputStream in = null;
		OutputStream out = null;

		BufferedInputStream bin = null;
		BufferedOutputStream bout = null;
		try {
			in = new FileInputStream(src);
			out = new FileOutputStream(target);
			bin = new BufferedInputStream(in);
			bout = new BufferedOutputStream(out);

			byte[] b = new byte[8192];
			int len = bin.read(b);
			while (len != -1) {
				bout.write(b, 0, len);
				len = bin.read(b);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bin != null) {
					bin.close();
				}
				if (bout != null) {
					bout.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static boolean newFolder(File dirFile) {
		try {
			if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
				boolean createdSuccess = dirFile.mkdirs();
				if (createdSuccess) {
					return true;
				} else {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
			return false;
		}
		return true;
	}
	
	public static boolean deleteFile(File file) {
		boolean result = false;
		if (file != null) {
			try {
				File file2 = file;
				file2.delete();
				result = true;
			} catch (Exception e) {
				e.printStackTrace();
				result = false;
			}
		}
		return result;
	}	
	
	
	public static void sortFiles(final List<File> files){
		Collections.sort(files, new Comparator<File>(){
			@Override
			public int compare(File file1, File file2) {
				String fileName1 = file1.getName();
				String fileName2 = file2.getName();
				return fileName1.compareToIgnoreCase(fileName2);
			}			
		});
		List<File> fileList = new ArrayList<File>();
		List<File> folderList = new ArrayList<File>();
		for(File file : files){
			if(file.isDirectory()){
				folderList.add(file);
			}else{
				fileList.add(file);
			}
		}
		files.clear();
		files.addAll(folderList);
		files.addAll(fileList);
	}
	
	public static String getFileEndingByFileName(String fileName){
		String end = "";
		int dotIndex = fileName.lastIndexOf(".");
	    if(dotIndex < 0){
	        return "";
	    }
	    end = fileName.substring(dotIndex).toLowerCase();
	    return end;
	}

	public static boolean isZIP(String fileName){
		if(fileName.endsWith(ZIP)){
			return true;
		}
		return false;
	}
}
